package com.zhiche.lisa.lspm.service.trailer.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.core.utils.Account.AccountUtil;
import com.zhiche.lisa.core.utils.Account.JwtAccountVO;
import com.zhiche.lisa.lspm.dao.mapper.trailer.TrailerLicenseAttachMapper;
import com.zhiche.lisa.lspm.dao.model.trailer.TrailerLicenseAttach;
import com.zhiche.lisa.lspm.dao.model.trailer.TrailerLicenseAttachHistory;
import com.zhiche.lisa.lspm.service.trailer.TrailerLicenseAttachHistoryService;
import com.zhiche.lisa.lspm.service.trailer.TrailerLicenseAttachService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 挂车证照附件 service impl
 * @since 2018-05-28
 * @author lbl
 */
@Service
public class TrailerLicenseAttachServiceImpl extends ServiceImpl<TrailerLicenseAttachMapper, TrailerLicenseAttach> implements TrailerLicenseAttachService {
@Autowired
private TrailerLicenseAttachHistoryService trailerLicenseAttachHistoryService;

    @Override
    public void batchInsert(List<TrailerLicenseAttach> list) {
        this.batchInsert(list);
    }

    @Override
    public void deleteBatchByIds(List<Integer> idList) {
        this.deleteBatchIds(idList);
    }

    @Override
    public TrailerLicenseAttach selectByVehicleLicenseId(Long vehicleLicenseId) {
        return  baseMapper.selectByVehicleLicenseId(vehicleLicenseId);
    }
    /**
     * 根据传入旧的附件信息进行删除
     * @param trailerLicenseAttach
     */
    @Override
    @Transactional
    public void updateOldAttachDeleteState(TrailerLicenseAttach trailerLicenseAttach) {
        EntityWrapper<TrailerLicenseAttachHistory> ewAttach = new EntityWrapper<>();
        ewAttach.eq("trailer_id", trailerLicenseAttach.getTrailerId());
        ewAttach.eq("license_id", trailerLicenseAttach.getLicenseId());
        ewAttach.eq("attach_type", trailerLicenseAttach.getAttachType());
        ewAttach.eq("attach_key", trailerLicenseAttach.getAttachKey());
        List<TrailerLicenseAttachHistory> carrierLicenseAttaches = trailerLicenseAttachHistoryService.selectList(ewAttach);
        if (CollectionUtils.isEmpty(carrierLicenseAttaches)) {
            TrailerLicenseAttachHistory trailerLicenseAttachHistory = new TrailerLicenseAttachHistory();
            BeanUtils.copyProperties(trailerLicenseAttach, trailerLicenseAttachHistory);
            trailerLicenseAttachHistory.setGmtCreate(new Date());
            trailerLicenseAttachHistory.setCreator(AccountUtil.getAccountInfoFromSecurityContext().getUsername());
            trailerLicenseAttachHistoryService.insert(trailerLicenseAttachHistory);
        }
        this.deleteById(trailerLicenseAttach.getId());
    }
}
